package sqlstuff;

import java.sql.*;

import javax.swing.JOptionPane;

public class SQLManage {
	Connection con = null;
	Statement stmt = null;

	public void setup() {
		try {
			con = DriverManager
					.getConnection("jdbc:sqlserver://titan.csse.rose-hulman.edu;DatabaseName=EFPAG;username ="
							+ "333Winter2014EFPAG"
							+ ";password = "
							+ "somethingsimple"
							+ ";");
			stmt = con.createStatement();
		} catch (SQLException e) {
			System.out.println("Db failed");
			e.printStackTrace();
		}
		System.out.println("Database connected!");
	}
	public ResultSet executeStatement(String SQL) {
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(SQL);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ResultSet executeSprocNoParams(String sprocName) {
		String[] array={};
		
		return executeSprocWithParams(sprocName,array);
	}
	
	public ResultSet executeSprocWithParams(String sprocName, String[] args) {
		try {
			ResultSet rs;
			StringBuilder s = new StringBuilder("exec [" + sprocName + "]");
			for(String arg:args){
				s.append(" "+arg);
			}
			s.append(";");
			System.out.println(s.toString());
			Statement stmt = this.con.createStatement();
			try{
			rs = stmt.executeQuery(s.toString());
			}catch(com.microsoft.sqlserver.jdbc.SQLServerException e){
//				System.err.println("SQL ERROR:");
//				System.out.println(e.toString());
				rs = null;
			}
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}